package com.bruce.tool.common.util.regex;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 功能:
 * 正则表达式
 * @author : wuyujia 2017/10/31 12:06
 * @author : bruce 2018/05/26 12:46
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class RegexUtils {

    /**
     * 非法字符正则校验
     */
    private static final Pattern CHINESE = Pattern.compile("[\\u4e00-\\u9fa5]");

    /**
     * 是否包含中文的校验
     * @param s 需要校验的字符串
     * @return true 包含, false 不包含
     */
    public static boolean containChinese(String s) {
        if (s != null && !s.isEmpty()) {
            Matcher matcher = CHINESE.matcher(s);
            if (matcher.find()) {
                return true;
            }
        }
        return false;
    }

    public static boolean containsMore(String string,String regex,Integer time){
        if( time <= 0 ){ return false; }
        Pattern p1 = Pattern.compile(regex);
        Matcher m1 = p1.matcher(string);
        Integer count = 0;
        do {
            count ++ ;
            if( count > time ){
                return true;
            }
        }while(m1.find());
        return false;
    }
}
